package com.jyh.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.jyh.config.BaseAction;
import com.jyh.config.quantity.Result;
import com.jyh.config.redis.JedisClient;
import com.jyh.service.LoginService;

@Controller
public class LoginController extends BaseAction {
	@Autowired
	private LoginService loginService;
	@Autowired
	private JedisClient jedisClientPool;

	@RequestMapping("/login")
	public ModelAndView welcome(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("login");
		String verifyLogin = request.getParameter("verify");
		mv.addObject("verify", verifyLogin);
		return mv;
	}

	/**
	 * 发送短信验证码
	 * 
	 * @param request
	 */
	@RequestMapping("login/valCode")
	public String getValCode(HttpServletRequest request) {
		Result result;
		String tel = request.getParameter("tel");// 手机号
		String pwd = request.getParameter("pwd");// 密码

		if (StringUtils.isEmpty(tel)) {
			result = new Result("9999", "用户名不能为空");
			return jsonOut(result);
		}
		if (StringUtils.isEmpty(pwd)) {
			result = new Result("9999", "密码不能为空");
			return jsonOut(result);
		}
		String key = "ValCode" + tel;
		String getKey = jedisClientPool.get(key);
		if (getKey != null) {
			jedisClientPool.del(key);
			result = new Result("9999", "请不要重复点击发送短信！");
			return jsonOut(result);
		}
		jedisClientPool.set(key, "1");
		// jedisClientPool.expire(key, 60*10);
		result = loginService.getValCode(tel, pwd);
		jedisClientPool.del(key);
		return jsonOut(result);
	}

	/**
	 * 验证登录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("login/Verify")
	public String getVerifyLogin(HttpServletRequest request, RedirectAttributes model, HttpSession session) {
		Result result = null;
		String tel = request.getParameter("username");// 手机号
		String pwd = request.getParameter("password");// 密码
		String note = request.getParameter("note");// 密码

		if (StringUtils.isEmpty(tel)) {
			result = new Result("9999", "用户名不能为空");
			model.addAttribute("verify", jsonOutSimplify(result));
			return "redirect:/login";
		}
		if (StringUtils.isEmpty(pwd)) {
			result = new Result("9999", "密码不能为空");
			model.addAttribute("verify", jsonOutSimplify(result));
			return "redirect:/login";
		}
		if (StringUtils.isEmpty(note)) {
			result = new Result("9999", "验证码不能为空");
			model.addAttribute("verify", jsonOutSimplify(result));
			return "redirect:/login";
		}
		result = loginService.getVerifyLogin(tel, pwd, note,request,session);
		model.addAttribute("verify", jsonOutSimplify(result));
		if ("0000".equals(result.getCode())) {
			return "frame/main";
		} else {
			return "redirect:/login";
		}
	}
}
